<template>
  <!-- 默认值弹框 -->
  <el-dialog
    width="600px"
    title="配置映射表"
    :visible.sync="dialogdefaultNum"
    :close-on-click-modal="false"
    :modal-append-to-body="false"
    append-to-body
    :before-close="onClose"
  >
    <el-button style="float: left; margin-bottom: 10px" :size="size" type="primary" @click="confirmdefaultNum"
    >确认</el-button
    >
    <el-table
      ref="multipleTable_default"
      :data="visitTableFieldList"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="defaultNumSelectionChange"
      v-loading="editloading"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column type="index" label="序号" width="120"> </el-table-column>
      <el-table-column prop="fieldName" label="字段" width="200">
        <template slot-scope="scope">
          <span>{{ scope.row.fieldName }}</span
          >({{ scope.row.fieldKey }})
        </template>
      </el-table-column>
      <el-table-column prop="address" label="默认值" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-select
            v-model="scope.row.defaultnumber"
            placeholder="请选择"
            :size="size"
            @change="chandefaultNumber(scope.$index, scope.row, $event)"
          >
            <el-option
              v-for="item in scope.row.fieldValues"
              :key="item.fieldValue"
              :value="item.fieldValue"
              :label="item.valueName"
            >
            </el-option>
          </el-select>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>
<script>
export default {
  props: {
    dialogdefaultNum: {
      type: Boolean,
      default: () => false,
    },
    visitTableFieldList: {
      type: Array,
      default: () => [],
    },
    defaultNumIndex: {
      type: Number,
      default: () => null
    },
    orignDefaultValues: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      defaultNumSelection: [],
      visitTableFieldCopyList: [],
      disabledSubmit: false,
      editloading: false,
      size: 'mini',
      addNumOpts: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    }
  },

  watch: {
    visitTableFieldList (val) {
      if (val) {
        this.$nextTick(() => {
          this.visitTableFieldCopyList = val
          this.$nextTick(() => {
            this.$refs.multipleTable_default && this.$refs.multipleTable_default.clearSelection()
            if (this.orignDefaultValues != null && this.orignDefaultValues.length > 0) {
              for (const role of this.orignDefaultValues) {
                // 默认值字段勾选
                for (const one of this.visitTableFieldList) {
                  if (role.tableField === one.fieldKey) {
                    one.defaultnumber = role.fieldValue
                    one.selectValue = role.fieldValue
                    one.fieldValues && one.fieldValues.forEach(item => {
                      if (item.fieldValue === one.selectValue) {
                        one.selectName = item.valueName
                      }
                    })
                    this.$refs.multipleTable_default.toggleRowSelection(one, true)
                  }
                }
              }
            }
          })

        })
      }
    },
    
  },

  methods: {
    // 默认值多选
    defaultNumSelectionChange(val) {
      this.defaultNumSelection = val
    },
    // 默认值弹框确认按钮
    confirmdefaultNum() {
      const def = []
      const defName = []
      console.log('this.defaultNumSelection', this.defaultNumSelection)
      for (const sel of this.defaultNumSelection) {
        def.push(sel.fieldName + ':' + sel.selectName)
        defName.push({
          tableField: sel.fieldKey,
          fieldValue: sel.defaultnumber,
        })
      }
      const obj = {
        defName,
        def,
        defaultNumIndex: this.defaultNumIndex
      }
      console.log('obj', obj)
      this.$emit('defaultOk', obj)
      // this.tableData[this.defaultNumIndex].default = def.join(';')
      // this.tableData[this.defaultNumIndex].defaultIds = defName
      // this.reftablerefresh++
    },
    // 默认值选择
    chandefaultNumber (index, data, event) {
      for (const item of data.fieldValues) {
        if (event === item.fieldValue) {
          this.visitTableFieldCopyList[index].selectValue = item.fieldValue
          this.visitTableFieldCopyList[index].selectName = item.valueName
        }
      }
    },
    onClose() {
      this.$emit('defaultCancal')
    },
  },
}
</script>
<style lang="scss" scoped>
.interview {
  color: #753cca;
  font-weight: bolder;
  text-align: right;
  font-size: 16px;
  padding-bottom: 10px;
  width: 130px;
}
</style>
